package cn.yuanqiao.web.controller.archive.appManager;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.yuanqiao.archive.appManager.domain.TenantCategory;
import cn.yuanqiao.archive.appManager.domain.TenantMdataGroup;
import cn.yuanqiao.archive.appManager.service.ITenantCategoryService;
import cn.yuanqiao.archive.appManager.service.InitArchiveAppService;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.poi.ExcelUtil;
import cn.yuanqiao.common.utils.SecurityUtils;
import com.alibaba.fastjson2.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;

import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;

/**
 * 业务档案门类Controller
 *
 * @author yuanqiao
 * @date 2023-06-14
 */
@RestController
@RequestMapping("/archive/category")
public class TenantCategoryController extends BaseController
{
    @Autowired
    private ITenantCategoryService categoryService;
    @Autowired
    private InitArchiveAppService archiveAppService;


    /**
     * 查询业务档案门类列表
     */
    //@PreAuthorize("@ss.hasPermi('archive:category:list')")
    @Log(title = "查询业务档案门类列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(TenantCategory tenantCategory)
    {
        //startPage();
        List<TenantCategory> list = categoryService.selectTenantCategoryList(tenantCategory);
        return getDataTable(list);
    }

    /**
     * 导出业务档案门类列表
     */
    //@PreAuthorize("@ss.hasPermi('archive:category:export')")
    //@Log(title = "业务档案门类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TenantCategory tenantCategory)
    {
        List<TenantCategory> list = categoryService.selectTenantCategoryList(tenantCategory);
        ExcelUtil<TenantCategory> util = new ExcelUtil<TenantCategory>(TenantCategory.class);
        util.exportExcel(response, list, "业务档案门类数据");
    }

    /**
     * 根据id获取档案门类信息
     * @author syd
     * @param ID
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('archive:category:query')")
    @Log(title = "根据id获取档案门类信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(categoryService.selectTenanCategoryByID(ID));
    }

    /**
     * 判断当前门类是否绑定元数据方案
     * @author syd
     * @param ID
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('archive:category:query')")
    @Log(title = "判断当前门类是否绑定元数据方案", businessType = BusinessType.SELECT)
    @GetMapping(value = "/isBindGroup/{ID}")
    public AjaxResult isBindGroup(@PathVariable("ID") Long ID)
    {
        TenantCategory tenantCategory = categoryService.selectTenanCategoryByID(ID);
        Long groupId = tenantCategory.getGroupId();
        if(StringUtils.isNotNull(groupId)){
            TenantMdataGroup bindGroup = categoryService.isBindGroup(groupId);
            return success(bindGroup);
        }
        return success("notbind");
    }

    /**
     * 添加门类
     * @author syd
     * @param tenantCategory
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('archive:category:add')")
    @Log(title = "添加门类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TenantCategory tenantCategory)
    {
        return toAjax(categoryService.insertTenantCategory(tenantCategory));
    }

    /**
     * 修改门类信息
     * @author syd
     * @param tenantCategory
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('archive:category:edit')")
    @Log(title = "修改门类信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TenantCategory tenantCategory)
    {
        return toAjax(categoryService.updateTenantCategory(tenantCategory));
    }

    /**


     * @author syd
     * @param IDs
     * @return
     */
    //@PreAuthorize("@ss.hasPermi('archive:category:remove')")
    @Log(title = "删除门类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(categoryService.deleteYqdacategoryModelByIDs(IDs));
    }

    /**
     * 档案门类获取树
     * @author syd
     * @param yqdacategorymodel
     * @return
     */
    @Log(title = "档案门类获取树", businessType = BusinessType.SELECT)
    @GetMapping("/getcatemodeltree")
    public AjaxResult getcatemodeltree(TenantCategory yqdacategorymodel){
        return success(categoryService.selectmatemodelTreeList(yqdacategorymodel));
    }


    @Log(title = "档案门类获取树元数据方案ID不为空的", businessType = BusinessType.SELECT)
    @GetMapping("/getcatemodeltreeUse")
    public AjaxResult getcatemodeltreeUse(){
        return success(categoryService.getcatemodeltreeUse());
    }

    /**
     * 档案门类获取未使用元数据方案
     * @author syd
     * @param
     * @return
     */
    @Log(title = "档案门类获取未使用元数据方案", businessType = BusinessType.SELECT)
    @GetMapping("/getNoUseGroups")
    public AjaxResult getNoUseGroups(){
        return success(categoryService.getNoUseGroups());
    }

    /**
     * 门类模板绑定元数据模板
     * @author syd
     * @param id
     * @param groupId
     * @param wjcls
     * @param ajcls
     * @param xmcls
     * @param type
     */
    ////@PreAuthorize("@ss.hasPermi('manage:MODEL:bind')")
    @Log(title = "门类绑定元数据方案", businessType = BusinessType.UPDATE)
    @PostMapping("/bindgroup")
    public AjaxResult bindgroup(@RequestParam Long id,@RequestParam Long groupId,@RequestParam Integer wjcls,@RequestParam Integer ajcls,@RequestParam Integer xmcls,@RequestParam Integer type)
    {
        return toAjax(categoryService.bindgroup(id,groupId,wjcls,ajcls,xmcls,type));
    }

    /**
     * 初始化档案门类
     * @param jsonArray
     * @return
     */
    @Log(title = "初始化档案门类", businessType = BusinessType.UPDATE)
    @PostMapping("/initCategory")
    public AjaxResult initCategory(@RequestBody JSONArray jsonArray){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        logger.info("业务数据初始化开始:用户信息="+loginUser.toString());
        logger.info("业务数据初始化参数:="+jsonArray.toJSONString());
        //[{"fond":"001","category":["1","2"]},{"fond":"002","category":["3","4"]}]
        String msg = archiveAppService.initArchiveApp(jsonArray);
        logger.info("业务数据初始化结果:"+msg);
        AjaxResult result = null;
        if(StringUtils.isEmpty(msg)){
            result = AjaxResult.success("200");
        }else {
            result = AjaxResult.error(msg);
        }
        return result;
    }

    /**
     * 获取门类 不分页
     */
    @Log(title = "查询业务档案门类列表", businessType = BusinessType.SELECT)
    @GetMapping("/listnopage")
    public AjaxResult listnopage(TenantCategory tenantCategory)
    {
        List<TenantCategory> list = categoryService.selectTenantCategoryList(tenantCategory);
        return success(list);
    }

    /**
     * 获取门类 不分页 必须要有元数据方案
     */
    @Log(title = "查询业务档案门类列表", businessType = BusinessType.SELECT)
    @GetMapping("/listnopagehasgroup")
    public AjaxResult listnopagehasgroup(TenantCategory tenantCategory)
    {
        List<TenantCategory> list = categoryService.listnopagehasgroup(tenantCategory);
        return success(list);
    }

    /**
     * 获取门类 不分页 必须要有元数据方案 根据档案树权限配置
     */
    @Log(title = "查询业务档案门类列表", businessType = BusinessType.SELECT)
    @GetMapping("/listnopagehasgrouphaspro")
    public AjaxResult listnopagehasgrouphaspro(@RequestParam(value="menuId",required = true)Long menuId,
                                               @RequestParam(value="type")Long type)
    {
        List<TenantCategory> list = categoryService.listnopagehasgrouphaspro(menuId, type);
        return success(list);
    }

    /**
     * 通过fondsid获取门类
     * @param fondsid
     * @return
     */
    @Log(title = "查询业务档案门类列表", businessType = BusinessType.SELECT)
    @GetMapping("/getcatebyfondsid/{fondsid}")
    public AjaxResult getcatebyfondsid(@PathVariable Long fondsid)
    {
        List<TenantCategory> list = categoryService.getcatebyfondsid(fondsid);
        return success(list);
    }
}
